typedef unsigned long uint64;

typedef struct buf{
    uint64 *data;
    size_t elem_num;
    size_t curr;
    struct buf *next, *prev;
} buf;

typedef struct filebuf {
    enum fb_type {FB_T_RD = 0,FB_T_WR,FB_T_EMPTY} type;
    enum fb_stat {FB_S_READY = 0, FB_S_IFIN, FB_S_FIN} status;
    /*
     * 状态转移:READY->IFIN (输入端已结束)
     *        将输出端所有数据读取完毕即可
     *        IFIN->FIN (过程结束)
     *        输出端所有数据已全部读出
     */
    int fd;
    pthread_mutex_t mtx;
    /* in:输入端可用; out:输出端可用; un:不可用 */
    buf *b_in, *b_out, *b_un; // buf ring
    sem_t ex_res; // 执行端(阻塞)
} filebuf;

typedef struct ring_node {
    filebuf *fbuf;
    struct ring_node *prev, *next;
} ring_node;

typedef struct filebuf_ring {
    ring_node *head, *tail;
    int elem_num;
    pthread_mutex_t mtx;
} filebuf_ring;

typedef struct filebuf_heap {
    filebuf **bufs;
    int num;
} filebuf_heap;

typedef struct threadinfo {
    pthread_t tid;
    pthread_attr_t attr;
    enum ti_type{TI_T_IO = 0, TI_T_MERGE, TI_T_MONITOR} type;
    enum ti_stat{TI_S_IDLE = 0, TI_S_LOADING, TI_S_STORING,
          TI_S_MERGING, TI_S_FINISH} status;
    int in_files;
    int out_files;
    int numa_id;
    int arg;
    int retval;
} threadinfo;